package com.ruoyi.app.controller.business.customer;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.collect.Lists;
import com.ruoyi.business.base.PageParam;
import com.ruoyi.business.customer.domain.JbFollowupEvaluation;
import com.ruoyi.business.customer.domain.dto.JbFollowupEvaluationDto;
import com.ruoyi.business.customer.domain.vo.JbFollowupEvaluationVo;
import com.ruoyi.business.customer.service.JbFollowupEvaluationService;
import com.ruoyi.business.sundry.domain.JbEvaluationExample;
import com.ruoyi.business.sundry.service.JbEvaluationExampleService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.web.core.JoR;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 跟进记录评价 Controller
 *
 * @author ruoyi
 * @date 2024-10-11
 */
@Api("跟进记录评价")
@RestController
@RequestMapping("/app/followup_evaluation")
public class AppJbFollowupEvaluationController extends BaseController
{
    @Autowired
    private JbFollowupEvaluationService service;
    @Autowired
    private JbEvaluationExampleService evaluationExampleService;
    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询跟进记录评价列表
     */
    @ApiOperation("查询跟进记录评价列表")
    @GetMapping("/list")
    public TableDataInfo list(PageParam page, JbFollowupEvaluation jbFollowupEvaluation)
    {
        LambdaQueryWrapper<JbFollowupEvaluation> select = Wrappers.lambdaQuery(jbFollowupEvaluation);
        select.orderByDesc(JbFollowupEvaluation::getId);
        IPage<JbFollowupEvaluation> iPage = service.page(page.b(), select);
        List<SysUser> userList = Lists.newArrayList();
        if (ObjectUtils.isNotEmpty(iPage) && ObjectUtils.isNotEmpty(iPage.getRecords()))
        {
            List<Long> createBy = iPage.getRecords().stream().map(JbFollowupEvaluation::getCreateBy)
                    .collect(Collectors.toList());
            userList = sysUserService.list(Wrappers.lambdaQuery(SysUser.class)
                    .in(SysUser::getUserId, createBy).select(SysUser::getUserId, SysUser::getUserName
                            , SysUser::getAvatar));
        }
        Map<Long, SysUser> userMap = userList.stream().collect(Collectors.toMap(SysUser::getUserId, Function.identity()));
        IPage<JbFollowupEvaluationVo> convert = iPage.convert(item ->
        {
            JbFollowupEvaluationVo vo = BeanUtil.toBean(item, JbFollowupEvaluationVo.class);
            SysUser sysUser = userMap.get(item.getCreateBy());
            if (ObjectUtils.isNotEmpty(sysUser))
            {
                vo.setCreateName(sysUser.getUserName());
                vo.setAvatar(sysUser.getAvatar());
            }
            return vo;
        });
        return JoR.getDataTable(convert);
    }

    /**
     * 导出跟进记录评价列表
     */
    @ApiOperation("导出跟进记录评价列表")
    @Log(title = "跟进记录评价", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PageParam page, JbFollowupEvaluation jbFollowupEvaluation)
    {
        LambdaQueryWrapper<JbFollowupEvaluation> select = new LambdaQueryWrapper<>();
        IPage<JbFollowupEvaluation> iPage = service.page(page.b(), select);

        List<JbFollowupEvaluation> list = iPage.getRecords();
        ExcelUtil<JbFollowupEvaluation> util = new ExcelUtil<JbFollowupEvaluation>(JbFollowupEvaluation.class);
        util.exportExcel(response, list, "跟进记录评价数据");
    }

    /**
     * 获取跟进记录评价详细信息
     */
    @ApiOperation("获取跟进记录评价详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(service.getById(id));
    }

    /**
     * 新增跟进记录评价
     */
    @ApiOperation("新增跟进记录评价")
    @Log(title = "跟进记录评价", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody JbFollowupEvaluationDto jbFollowupEvaluationDto)
    {
        return toAjax(service.saveInfo(jbFollowupEvaluationDto));
    }

    /**
     * 修改跟进记录评价
     */
    @ApiOperation("修改跟进记录评价")
    @Log(title = "跟进记录评价", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody JbFollowupEvaluation jbFollowupEvaluation)
    {
        return toAjax(service.updateById(jbFollowupEvaluation));
    }

    /**
     * 删除跟进记录评价
     */
    @ApiOperation("删除跟进记录评价")
    @Log(title = "跟进记录评价", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        boolean b = service.removeByIds(Arrays.asList(ids));
        if (b)
        {
            evaluationExampleService.remove(Wrappers.lambdaQuery(JbEvaluationExample.class).in(JbEvaluationExample::getEvaluationId, ids));
        }
        return toAjax(b);
    }
}
